package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Classname AddressController
 * @Description TODO
 * @Date 2023/3/9 15:30
 * @Created by 11404
 */

@Api(tags = "APP地址管理")
@Slf4j
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    public AddressService addressService;


    // 查询收货地址列表
    /*
        请求方式:GET
        请求地址：http://localhost:8081/address/list
        返回值： List<Address>
     */

    @ApiOperation(value = "查询收货地址列表")
    @GetMapping("/list")
    public ResultInfo list() {

        List<Address> lists = addressService.listAddress();

        return ResultInfo.success(lists);
    }

    // 新增收货地址
    @ApiOperation(value = "新增收货地址")
    @PostMapping
    public ResultInfo adds(@RequestBody Address address) {

        addressService.add(address);

        return ResultInfo.success();
    }

    // 设置默认地址
    @ApiOperation(value = "设置默认地址")
    @PutMapping("/default")
    public ResultInfo setDefault(@RequestBody Address address) {

        addressService.setDefault(address);

        return ResultInfo.success();
    }

    // 查询默认地址
    @ApiOperation(value = "查询默认地址")
    @GetMapping("/default")
    public ResultInfo findDefault() {
        Address address = addressService.findDefault();
        return ResultInfo.success(address);
    }

    //回显地址 根据ID查
    @ApiOperation(value = "ID查询收货地址")
    @GetMapping("/{id}")
    public ResultInfo addById(@PathVariable Long id) {

        Address address = addressService.addId(id);

        return ResultInfo.success(address);

    }

    //修改地址  update
    @ApiOperation(value = "修改收货地址")
    @PutMapping
    public ResultInfo update(@RequestBody Address address) {

        addressService.updateAdd(address);

        return ResultInfo.success();
    }


}
